﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using EveKillboard.Core.Services;
using EveKillboard.Data;
using EveKillboard.Services.EveKillServices;
using System.Diagnostics;

namespace KillmailsHistoryGrabber
{
    class Program
    {
        static void Main(string[] args)
        {
            Trace.Listeners.Add(new ConsoleTraceListener());

            var characterId = 0;
            var corpId = 0;
            var allyId = 0;
            DateTime startDate = DateTime.MinValue;
            DateTime endDate = startDate;

            bool hasDate = false;
            bool isCharacter = false;
            bool isCorp = false;
            bool isAlly = false;

            for(int i=0; i < args.Length; i++)
            {
                if(args[i].StartsWith("-corp:"))
                {
                    corpId = int.Parse(args[i].Split(':')[1]);
                    isCorp = corpId > 0;
                    continue;
                }
                if(args[i].StartsWith("-char:"))
                {
                    characterId = int.Parse(args[i].Split(':')[1]);
                    isCharacter = characterId > 0;
                    continue;
                }
                if (args[i].StartsWith("-ally:"))
                {
                    allyId = int.Parse(args[i].Split(':')[1]);
                    isAlly = allyId > 0;
                    continue;
                }
                if(args[i].StartsWith("-sdate:"))
                {
                    startDate = DateTime.ParseExact(args[i].Split(':')[1], "yyyyMMdd", CultureInfo.InvariantCulture);
                    hasDate = true;
                    continue;
                }
                if (args[i].StartsWith("-edate:"))
                {
                    endDate = DateTime.ParseExact(args[i].Split(':')[1], "yyyyMMdd", CultureInfo.InvariantCulture);
                    hasDate = true;
                    continue;
                }
            }
            
            if(!hasDate)
            {
                Console.WriteLine("No date specified");
                return;
            }
            
            if(endDate == DateTime.MinValue)
                endDate = DateTime.Today;

            if(startDate > endDate)
            {
                Console.WriteLine("Start date cannot be greater than end date");
                return;
            }

            if(!isCharacter && !isCorp && !isAlly)
            {
                Console.WriteLine("No Alliance, Corporation or Character id specified");
                return;
            }
            
            var evekillsvc = new KillmailFeedProvider();
            var kills = new List<IFeedKillmail>();

            if (isCharacter)
            {
                Console.WriteLine("Retrieving character kills");
                var charKills = evekillsvc.GetKillmails(characterId,KillmailFeedProvider.IdTypesEnum.Character, startDate, endDate);
                Console.WriteLine("{0} character kills retrieved", charKills);
                kills.AddRange(charKills);
            }
            if (isCorp)
            {
                Console.WriteLine("Retrieving corporation kills");
                var corpKills = evekillsvc.GetKillmails(corpId,KillmailFeedProvider.IdTypesEnum.Corporation, startDate, endDate);
                Console.WriteLine("{0} corporation kills retrieved", corpKills);
                kills.AddRange(corpKills);
            }
            if(isAlly)
            {
                Console.WriteLine("Retrieving Alliance kills");
                var allykills = evekillsvc.GetKillmails(allyId, KillmailFeedProvider.IdTypesEnum.Alliance, startDate,endDate);
                Console.WriteLine("{0} alliance kills retrieved", allykills);
                kills.AddRange(allykills);
            }
            evekillsvc.Dispose();
            evekillsvc = null;
            
            GC.Collect();

            var worker = new EveKillboard.Core.KillmailWorker()
                             {
                                 ApiServices = new EveApiServicesProvider(),
                                 DataAccessProvider = new DataAccessProvider(),
                                 ItemPriceRefreshDelay = 3,
                                 PricesProvider = new EvePriceServicesProvider()
                             };

            int total = kills.Count();

            if(total == 0)
            {
                Console.WriteLine("No killmails to process");
                return;
            }

            Console.WriteLine("{0} killmails received", total);

            int savedCount = 0;
            foreach (var feedKillmail in kills)
            {
                try
                {
                    worker.SaveKillmail(feedKillmail);
                    savedCount++;

                    Console.Clear();
                    Console.WriteLine("{1} killmails saved on {2} ({0:P2})", ((decimal)savedCount / (decimal)total), savedCount, total);
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.Message);
                }
            }
            
            Console.Clear();
            Console.WriteLine("{1} killmails saved on {2} ({0:P2})", ((decimal)savedCount / (decimal)total), savedCount, total);
        }
    }
}
